package handle.functions;

import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import response.MessageType.Message_Article;
import response.MessageType.Message_Image;
import response.MessageType.Message_Text;
import response.MessageType.Message_Voice;
import response.MessageType.Message_Music;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;

public class MessageUtil {
	public static final String REQ_MESSAGETYPE_TEXT = "text";
	public static final String REQ_MESSAGETYPE_IMAGE = "image";
	public static final String REQ_MESSAGETYPE_VOICE = "voice";
	public static final String REQ_MESSAGETYPE_VIDEO = "video";
	public static final String REQ_MESSAGETYPE_LOCATION = "location";
	public static final String REQ_MESSAGETYPE_LINK = "link";
	
	public static final String REQ_MESSAGETYPE_EVENT = "event"; //event
	public static final String EVENT_TYPE_SUBSCRIBE = "subscribe"; //dingyue
	public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe"; //
	public static final String EVENT_TYPE_SCAN = "scan"; //QC
	public static final String EVENT_TYPE_LOCATION = "LOCATION"; //
	public static final String EVENT_TYPE_CLICK = "CLICK"; //Munu Click
	
	public static final String RESP_MESSAGETYPE_TEXT = "text";
	public static final String RESP_MESSAGETYPE_IMAGE = "image";
	public static final String RESP_MESSAGETYPE_VOICE = "voice";
	public static final String RESP_MESSAGETYPE_VIDEO = "video";
	public static final String RESP_MESSAGETYPE_MUSIC = "music";
	public static final String RESP_MESSAGETYPE_ARTICLE = "article";
	
	/*
	 * put into map, wait for doPost
	 * 
	 * */
	public static Map<String, String> parseXML(HttpServletRequest request)
			throws Exception{
			Map<String, String> map = new HashMap<String, String>();
			InputStream iStream = request.getInputStream();
			/*DOM4J*/
			SAXReader reader = new SAXReader();
			Document document = reader.read(iStream);
			//get root
			Element root = document.getRootElement();
			List<Element> elementlist = root.elements();
			//read all
			for(Element e:elementlist){
				map.put(e.getName(), e.getText());
			}
			iStream.close();
			iStream = null;
			return map;
	}
	
	/*-----CDATA----
	 * 	 * <  &  is not allowed
	 * forE <content>if age < 18 then</content>
	 * <content><![CDATA[if age < 18 then]]</content>
	 * */
	private static XStream xStream = new XStream(new Xpp3Driver(){
		public HierarchicalStreamWriter createWriter(Writer writer){
			return new PrettyPrintWriter(writer){
				boolean cdata = true;
				
				@SuppressWarnings("unchecked")
				public void startNOde(String name, Class clazz){
					super.startNode(name, clazz);
				}
				protected void writeText(QuickWriter writer, String text){
					if(cdata){
						writer.write("<![CDATA");
						writer.write(text);
						writer.write("]]>");
					}else{
						writer.write(text);
					}
				}
			};
		}
	});
	//message To XML
	public static String messageToXML(Message_Text text){
		xStream.alias("xml", text.getClass());
		return xStream.toXML(text);
	}
	public static String messageToXML(Message_Image image){
		xStream.alias("xml", image.getClass());
		return xStream.toXML(image);	
	}
	public static String messageToXML(Message_Voice voice){
		xStream.alias("xml", voice.getClass());
		return xStream.toXML(voice);
	}
	public static String messageToXML(Message_Music music){
		xStream.alias("xml", music.getClass());
		return xStream.toXML(music);
	}
	public static String messageToXML(Message_Article article){
		xStream.alias("xml", article.getClass());
		return xStream.toXML(article);
	}
}
